/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPelayananResepLuar.java
 *
 * Created on Jan 25, 2012, 10:32:03 AM
 */
package SIMRS.Apotek.view;

import SIMRS.Apotek.sql.KoneksiDatabase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import usu.widget.Form;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Maulida
 */
public class frmPelayananResepLuar extends Form {
    private static DefaultTableModel model;
        Connection c=KoneksiDatabase.getKoneksi();
        
    /** Creates new form frmPelayananResepLuar */
    public frmPelayananResepLuar() {
        initComponents();
        model=new DefaultTableModel();
        tblPelayananResepLuar.setModel(model);
        model.addColumn("Kode Obat");
        model.addColumn("Nama Obat");
        model.addColumn("Jumlah");
        model.addColumn("Harga");
        
        txtHarga.setEnabled(false);
        
        Date tanggal = new Date();
        txtTanggalResep.setDate(tanggal);
        
        cmbNamaObat.addItem("");
        try {
            Statement s=c.createStatement();
            ResultSet rs=s.executeQuery("select NamaBarang from obat order by NamaBarang asc");
            rs.beforeFirst();
            while (rs.next()) {                
                cmbNamaObat.addItem(rs.getString("NamaBarang"));
            }
            rs.close();
        } catch (SQLException ex) {
            System.out.println("Ada eror: "+ex);
        }
       
        
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/icon.png")).getImage());
        setTitle(".:: Form Pelayanan Resep Luar");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlForm1 = new SIMRS.Apotek.widget.PnlForm();
        label9 = new SIMRS.Apotek.widget.Label();
        txtNama = new SIMRS.Apotek.widget.TextBox();
        label4 = new SIMRS.Apotek.widget.Label();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAlamat = new SIMRS.Apotek.widget.TextArea();
        label6 = new SIMRS.Apotek.widget.Label();
        txtUmur = new SIMRS.Apotek.widget.TextBox();
        label7 = new SIMRS.Apotek.widget.Label();
        pnlPelayananResepLuar = new javax.swing.JPanel();
        scrollPane1 = new SIMRS.Apotek.widget.ScrollPane();
        tblPelayananResepLuar = new SIMRS.Apotek.widget.Table();
        btnTambah = new SIMRS.Apotek.widget.button2();
        btnHapus = new SIMRS.Apotek.widget.button2();
        label8 = new SIMRS.Apotek.widget.Label();
        txtTotal = new SIMRS.Apotek.widget.TextBox();
        label2 = new SIMRS.Apotek.widget.Label();
        cmbNamaObat = new javax.swing.JComboBox();
        label3 = new SIMRS.Apotek.widget.Label();
        label5 = new SIMRS.Apotek.widget.Label();
        txtJml = new SIMRS.Apotek.widget.TextBox();
        txtHarga = new SIMRS.Apotek.widget.TextBox();
        KdBarang = new javax.swing.JTextField();
        label13 = new SIMRS.Apotek.widget.Label();
        btnCetak = new SIMRS.Apotek.widget.button2();
        btnSimpan = new SIMRS.Apotek.widget.button2();
        btnTutup = new SIMRS.Apotek.widget.button2();
        txtTanggalResep = new com.toedter.calendar.JDateChooser();
        label1 = new SIMRS.Apotek.widget.Label();
        txtNoResep = new SIMRS.Apotek.widget.TextBox();

        setTitle("Form Pelayanan Resep Luar");
        setAnimationHide(usu.widget.constan.Animation.SHOW_FROM_BOTTOM);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_TOP);
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlForm1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(57, 190, 53), 3, true));
        pnlForm1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        label9.setText("Nama Pasien");
        pnlForm1.add(label9, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 200, 100, 20));

        txtNama.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNamaActionPerformed(evt);
            }
        });
        pnlForm1.add(txtNama, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 220, 170, -1));

        label4.setText("Alamat");
        pnlForm1.add(label4, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 140, 100, 20));

        txtAlamat.setColumns(20);
        txtAlamat.setRows(5);
        jScrollPane2.setViewportView(txtAlamat);

        pnlForm1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 160, -1, 50));

        label6.setText("Umur");
        pnlForm1.add(label6, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 140, 50, 20));

        txtUmur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUmurActionPerformed(evt);
            }
        });
        pnlForm1.add(txtUmur, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 160, 70, -1));

        label7.setText("Tanggal Resep");
        pnlForm1.add(label7, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 140, 90, 20));

        pnlPelayananResepLuar.setBackground(new java.awt.Color(51, 51, 51));
        pnlPelayananResepLuar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlPelayananResepLuar.setOpaque(false);
        pnlPelayananResepLuar.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblPelayananResepLuar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrollPane1.setViewportView(tblPelayananResepLuar);

        pnlPelayananResepLuar.add(scrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 70, 620, 180));

        btnTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_tambah.png"))); // NOI18N
        btnTambah.setText("Tambah");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });
        pnlPelayananResepLuar.add(btnTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 260, -1, -1));

        btnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_hapus.png"))); // NOI18N
        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        pnlPelayananResepLuar.add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 260, -1, -1));

        label8.setText("Total");
        pnlPelayananResepLuar.add(label8, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 270, 30, 20));
        pnlPelayananResepLuar.add(txtTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 270, 120, -1));

        label2.setText("Nama Obat");
        pnlPelayananResepLuar.add(label2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 10, -1, -1));

        cmbNamaObat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbNamaObatActionPerformed(evt);
            }
        });
        pnlPelayananResepLuar.add(cmbNamaObat, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 150, -1));

        label3.setText("Jumlah");
        pnlPelayananResepLuar.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 10, -1, -1));

        label5.setText("Harga");
        pnlPelayananResepLuar.add(label5, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 10, -1, -1));
        pnlPelayananResepLuar.add(txtJml, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 30, 60, -1));
        pnlPelayananResepLuar.add(txtHarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 30, 80, -1));

        KdBarang.setForeground(new java.awt.Color(255, 255, 153));
        KdBarang.setBorder(null);
        KdBarang.setFocusable(false);
        KdBarang.setOpaque(false);
        pnlPelayananResepLuar.add(KdBarang, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 80, -1));

        pnlForm1.add(pnlPelayananResepLuar, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 250, 680, 300));

        label13.setForeground(new java.awt.Color(255, 255, 255));
        label13.setText("Pelayanan Resep Luar");
        label13.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label13, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 40, 400, -1));

        btnCetak.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_cetak.png"))); // NOI18N
        btnCetak.setText("Cetak Resep");
        pnlForm1.add(btnCetak, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 560, -1, -1));

        btnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_simpan.png"))); // NOI18N
        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        pnlForm1.add(btnSimpan, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 570, -1, -1));

        btnTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_close.png"))); // NOI18N
        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });
        pnlForm1.add(btnTutup, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 570, -1, -1));

        txtTanggalResep.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                txtTanggalResepAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        pnlForm1.add(txtTanggalResep, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 160, 140, -1));

        label1.setText("No Resep");
        pnlForm1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 150, -1, -1));
        pnlForm1.add(txtNoResep, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 170, 100, -1));

        getContentPane().add(pnlForm1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 760, 620));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-760)/2, (screenSize.height-615)/2, 760, 615);
    }// </editor-fold>//GEN-END:initComponents

private void txtNamaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNamaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNamaActionPerformed

private void txtUmurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUmurActionPerformed
// TODO add your handling code here:
 /*   Connection c=KoneksiDatabase.getKoneksi();
        try {
            Statement s=c.createStatement();
            String sql="SELECT HitungUmur(TanggalLahir);";
            
            ResultSet rs=s.executeQuery(sql);
       //     txtUmur.setText(sql);
            System.out.println("umurnya "+rs);
        } catch (SQLException ex) {
            
        }*/
}//GEN-LAST:event_txtUmurActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
    System.exit(1);
}//GEN-LAST:event_formWindowClosing

private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
    dispose();
}//GEN-LAST:event_btnTutupActionPerformed

private void txtTanggalResepAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_txtTanggalResepAncestorAdded
// TODO add your handling code here:
    
}//GEN-LAST:event_txtTanggalResepAncestorAdded

private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
// TODO add your handling code here:
    int row=tblPelayananResepLuar.getSelectedRow();
    model.removeRow(row); 
}//GEN-LAST:event_btnHapusActionPerformed

    private void cmbNamaObatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbNamaObatActionPerformed
        // TODO add your handling code here:
        try{
         Statement stm = c.createStatement();
         ResultSet rs = stm.executeQuery("SELECT obat.KdBarang, hargabarangmedis.HargaNetto1 FROM obat JOIN hargabarangmedis ON (obat.KdBarang=hargabarangmedis.KdBarang) where obat.NamaBarang= '"+cmbNamaObat.getSelectedItem().toString()+"'");
         rs.beforeFirst();
         while(rs.next()){
             KdBarang.setText(rs.getString(1).trim());
             txtHarga.setText(rs.getString(2).trim());
         }
         rs.close();
         }
         catch(SQLException ex)
         {
             System.out.println("Error : "+ex);
             }
    }//GEN-LAST:event_cmbNamaObatActionPerformed

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed
        // TODO add your handling code here:
        String []a=new String[4];
        a[0]=KdBarang.getText();
        a[1]=cmbNamaObat.getSelectedItem().toString();
        a[2]=txtJml.getText();
        a[3]=txtHarga.getText();
          
        model.addRow(a); 
        hitung_total(); 
    }//GEN-LAST:event_btnTambahActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        // TODO add your handling code here:
        if (tblPelayananResepLuar.getRowCount() == 0) {
            JOptionPane.showMessageDialog(null, "Data Masih Kosong!!", "Warning", JOptionPane.INFORMATION_MESSAGE);
        } else {
            try {
            String noResep = txtNoResep.getText();
            String namaPembeli= txtNama.getText();
            String alamat= txtAlamat.getText();
            String umur = txtUmur.getText();
            SimpleDateFormat Format = new SimpleDateFormat("yyyy-MM-dd");
            String tanggal = Format.format(txtTanggalResep.getDate());
         //   String namaObat= KdBarang.getText();
        //    String jml= txtJml.getText();
            String total = txtTotal.getText();
            
            Statement stm = c.createStatement();
            String sql = "insert into pembeliobatluar values('" + noResep + "','" + namaPembeli + "','" + alamat + "','" + umur + "')";
           
            stm.executeUpdate(sql);
           
            for (int i = 0; i < tblPelayananResepLuar.getRowCount(); i++) {

                    String barang = (String) tblPelayananResepLuar.getValueAt(i, 0);
                    String jumlah= (String) tblPelayananResepLuar.getValueAt(i, 2);
                    String harga= (String) tblPelayananResepLuar.getValueAt(i, 3);
                    
                    String sql1 = "insert into transaksiresepluar values('" + noResep + "','" + barang + "','" + jumlah + "','" + harga + "','" + total +"','" +tanggal+"')";

                    stm.executeUpdate(sql1);

           }
            
            JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan");
           
            txtNoResep.setText("");
            txtNama.setText("");
            txtAlamat.setText("");
            txtUmur.setText("");
            txtJml.setText("");
            txtHarga.setText("");
            txtTotal.setText("");
            KdBarang.setText("");
            
            cmbNamaObat.setSelectedItem("");
            
        } catch (SQLException e) {
            System.out.println("Error : " + e);
          //  JOptionPane.showConfirmDialog(null, "Penginputan Error..!!", ".:Information", JOptionPane.OK_OPTION);
            JOptionPane.showMessageDialog(this, "Data gagal disimpan");
        }
    }
    }//GEN-LAST:event_btnSimpanActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField KdBarang;
    private SIMRS.Apotek.widget.button2 btnCetak;
    private SIMRS.Apotek.widget.button2 btnHapus;
    private SIMRS.Apotek.widget.button2 btnSimpan;
    private SIMRS.Apotek.widget.button2 btnTambah;
    private SIMRS.Apotek.widget.button2 btnTutup;
    private javax.swing.JComboBox cmbNamaObat;
    private javax.swing.JScrollPane jScrollPane2;
    private SIMRS.Apotek.widget.Label label1;
    private SIMRS.Apotek.widget.Label label13;
    private SIMRS.Apotek.widget.Label label2;
    private SIMRS.Apotek.widget.Label label3;
    private SIMRS.Apotek.widget.Label label4;
    private SIMRS.Apotek.widget.Label label5;
    private SIMRS.Apotek.widget.Label label6;
    private SIMRS.Apotek.widget.Label label7;
    private SIMRS.Apotek.widget.Label label8;
    private SIMRS.Apotek.widget.Label label9;
    private SIMRS.Apotek.widget.PnlForm pnlForm1;
    private javax.swing.JPanel pnlPelayananResepLuar;
    private SIMRS.Apotek.widget.ScrollPane scrollPane1;
    private SIMRS.Apotek.widget.Table tblPelayananResepLuar;
    private SIMRS.Apotek.widget.TextArea txtAlamat;
    private SIMRS.Apotek.widget.TextBox txtHarga;
    private SIMRS.Apotek.widget.TextBox txtJml;
    private SIMRS.Apotek.widget.TextBox txtNama;
    private SIMRS.Apotek.widget.TextBox txtNoResep;
    private com.toedter.calendar.JDateChooser txtTanggalResep;
    private SIMRS.Apotek.widget.TextBox txtTotal;
    private SIMRS.Apotek.widget.TextBox txtUmur;
    // End of variables declaration//GEN-END:variables

public void isiText(String noResep, String namaPembeli, String alamat, String umur, Date TglResep, String NamaBarang, String JmlBarang, String harga, String TotalHarga){
  txtNoResep.setText(noResep);
  txtNama.setText(namaPembeli);
  txtAlamat.setText(alamat);
  txtUmur.setText(umur);
  txtTanggalResep.setDate(TglResep);
  cmbNamaObat.setSelectedItem(NamaBarang);
  txtJml.setText(JmlBarang);
  txtHarga.setText(harga);
//  txtTotal.setText(TotalHarga);
   
}

private float hitung_total(){
       
       int jml, hrga;
       float xtot;
       
       xtot=0;
       jml = Integer.parseInt(txtJml.getText());
       hrga=Integer.parseInt(txtHarga.getText());
       xtot = jml*hrga;
       for (int i = 0; i < tblPelayananResepLuar.getRowCount(); i++) {
           xtot=xtot+xtot;
       }
       
    //   tblPelayananResepLuar.getCellRenderer(jml, 3);
       
       String xjml = Float.toString(xtot);
       txtTotal.setText(xjml);
 
       return xtot;
        
    }



}
